<?php
/**
 * Created by
 * User: xuguopeng
 * Date: 7/10/21
 * Time: 10:35 PM
 * Effect: 官方活动类
 */

namespace app\api\controller\v1;


use app\api\model\GfActivity as GfActivityModel;

use app\api\model\GfActivityImg;
use LinCmsTp5\exception\ParameterException;
use think\Db;

class GfActivity
{
    /**
     * 获取全部
     */
    public function getDataAll()
    {
        return GfActivityModel::with('imgs.img')->order('id desc')->select()->toArray();
    }

    /**
     * 获取列表
     * @validate('PagingParameter')
     */
    public function getDataList($title,$page,$limit)
    {
        $where = [];
        if($title) {
            $where[] = ['title','like','%'.$title.'%'];
        }
        $query = GfActivityModel::where($where)->with('imgs.img')->order('order_sort desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function (&$item){
            if($item['recommend'] == 0) {
                $item['recommend'] = '不推荐';
            } else {
                $item['recommend'] = '推荐';
            }
            if($item['num_status'] == 0) {
                $item['num_status'] = '不限制';
            } elseif($item['num_status'] == 1) {
                $item['num_status'] = '限人数';
            } elseif($item['num_status']) {
                $item['num_status'] = '限性别';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取详情
     * @validate('IdMustBePositiveInt')
     */
    public function getDataInfo($id)
    {
        return GfActivityModel::where('id',$id)->with('imgs.img')->find();
    }

    /**
     * 添加
     * @validate('CheckGfActivity')
     */
    public function createData($title,$recommend,$order_sort,$img_ids,$price,$number,$men_num,$women_num,
                               $start_time,$end_time,$content,$zy_content,$num_status)
    {
        Db::startTrans();
        try{
            $mode = GfActivityModel::create([
                'title' => $title,
                'recommend' => $recommend,
                'price' => $price,
                'order_sort' => $order_sort,
                'num_status' => $num_status,
                'number' => $number,
                'men_num' => $men_num,
                'women_num' => $women_num,
                'start_time' => strtotime($start_time),
                'end_time' => strtotime($end_time),
                'content' => $content,
                'zy_content' => $zy_content,
            ]);
            $img_data = [];
            foreach ($img_ids as $k=>$v){
                $img_data[$k] = ['img_id'=>$v['imgId']];
            }
            $mode->imgs()->saveAll($img_data);
            Db::commit();
            return writeJson(201, [], '添加成功！');
        } catch (\Exception $e){
            Db::rollback();
            throw new ParameterException([
                'msg' => [
                    'product' => '添加失败!'
                ]
            ]);
        }
    }

    /**
     * 修改信息
     * @validate('CheckGfActivity')
     */
    public function updateData($id,$title,$recommend,$order_sort,$num_status,$img_ids,$price,$number,$men_num,$women_num,
                               $start_time,$end_time,$content,$zy_content)
    {
        Db::startTrans();
        try {
            $info = GfActivityModel::get($id);
            if ($info) {
                $info->title = $title;
                $info->recommend = $recommend;
                $info->order_sort = $order_sort;
                $info->price = $price;
                $info->num_status = $num_status;
                if($info->num_status == 0) {
                    $info->number = null;
                    $info->men_num = null;
                    $info->women_num = null;
                } elseif($info->num_status == 1) {
                    $info->number = $number;
                    $info->men_num = null;
                    $info->women_num = null;
                } elseif($info->num_status == 2) {
                    $info->number = $men_num + $women_num;
                    $info->men_num = $men_num;
                    $info->women_num = $women_num;
                }
                $info->start_time = strtotime($start_time);
                $info->end_time = strtotime($end_time);
                $info->content = $content;
                $info->zy_content = $zy_content;
                $info->save();
                GfActivityImg::destroy([
                    'activity_id' => $id
                ]);
                $img_data = [];
                foreach ($img_ids as $k=>$v){
                    $img_data[$k] = [
                        'img_id'=>$v['imgId'],
                        'activity_id' => $info->id
                    ];
                }
                $GfActivityImg = new GfActivityImg();
                $GfActivityImg->allowField(true)->saveAll($img_data);
            }
            Db::commit();
            return writeJson(202, [], '修改成功！');
        }catch (\Exception $e){
            throw new ParameterException([
                'msg' => [
                    'product' => '修改失败!'
                ]
            ]);
        }
    }

    /**
     * @validate('IdMustBePositiveInt')
     */
    public function deleteData($id)
    {
        GfActivityModel::destroy($id);
        GfActivityImg::destroy(['activity_id'=>$id]);
        return writeJson(203, [], '删除成功！');
    }

}